<?php

namespace App\Services\Game\Other\Mail;

use App\Services\Service;
use App\Services\Traits\DatabaseMysql;
use App\Mappers\Game\Other\Mail\ServerMapper as DataMapper;
use App\Models\Game\Mail\Server as Mail;
use App\Models\Game\Mail\ServerDictionary;
use App\Models\Game\Common\Dictionary;

class ServerService extends Service
{
    use DatabaseMysql;

    /**
     * 获取数据列表
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Database\Eloquent\Collection[]|int[]
     */
    public function getList($request)
    {
//        $query = Mail::query()->where('plat_code', $request->user()->config->org->code);
        $query = Mail::query();

        // 筛选【平台】
        $this->makeWhereParamOrg($request, $query);
        // 筛选【时间】
        $this->makeWhereParamDatetime($request, $query);
        // 筛选【类型】
        $this->makeWhereParam($request, 'type', $query);
        // 筛选【是否发送】
        $this->makeWhereParam($request, 'issend', $query);

        // 数据条数
        $total = (clone $query)->count();
        // 排序分页
        $this->makeOrderParam($request, $query)->makePaginationParam($request, $query);
        // 查询数据
        $collection = $query->with(['org:code,name', 'titles:id,content', 'bodys:id,content'])->get();

        return [
            $collection,
            $total
        ];
    }

    /**
     * 创建数据
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Database\Eloquent\Model
     */
    public function store($request)
    {
        // 验证内容和参数匹配
        Dictionary::validate(
            $request->input('body_ids', []),
            $request->input('body_param', [])
        );

        // 数据映射
        $data = DataMapper::make($request);

        // 创建数据
        $mail = Mail::create($data->toArray());
        // 标题
        $mail->titles()->sync(array_fill_keys(
            $request->input('title_ids', []),
            ['mail_content_type' => ServerDictionary::TYPE_TITLE]
        ));
        // 内容
        $mail->bodys()->sync(array_fill_keys(
            $request->input('body_ids', []),
            ['mail_content_type' => ServerDictionary::TYPE_BODY]
        ));

        return $mail;
    }

    /**
     * 更新数据
     *
     * @param int $id
     * @param \Illuminate\Http\Request $request
     * @return \App\Models\Game\Mail\Server
     */
    public function update($id, $request)
    {
        // 验证内容和参数匹配
        Dictionary::validate(
            $request->input('body_ids', []),
            $request->input('body_param', [])
        );

        // 数据映射
        $data = DataMapper::make($request);

        // 查询数据
        $mail = Mail::findOrFail($id);
        // 修改数据
        $mail->fill($data->toArray())->save();

        // 标题
        $mail->titles()->sync(array_fill_keys(
            $request->input('title_ids', []),
            ['mail_content_type' => ServerDictionary::TYPE_TITLE]
        ));
        // 内容
        $mail->bodys()->sync(array_fill_keys(
            $request->input('body_ids', []),
            ['mail_content_type' => ServerDictionary::TYPE_BODY]
        ));

        return $mail;
    }
}
